<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Product;
use App\Cate;
use App\Http\Requests\CreateProductRequest;
use App\Http\Requests\UpdateProductRequest;
use MyFlash;
use Illuminate\Support\Facades\Storage;
class ProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $list=Product::orderBy('id','desc')->paginate(15);
        return view('admin.product.index',compact('list'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $cates=Cate::getCates();
        return view('admin.product.create',compact('cates'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CreateProductRequest $request)
    {
        $product=new Product;
        $product->cid=$request->cid;
        $product->title=$request->title;
        $product->keywords=$request->keywords;
        $product->desc=$request->desc;
        $product->remark=$request->remark;
        $product->views=$request->views;
        $product->content=$request->contents;

        $product->pic=$this->picUploads($request);
        if(isset($request->isshow)){
            $product->isshow=$request->isshow;
        }else{
            $product->isshow=0;
        }
        if($product->save()){
            MyFlash::success('产品添加成功');
            return redirect()->route('admin.product.index');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $product=Product::findOrFail($id);
        $cates=Cate::getCates();
        return view('admin.product.edit',compact('cates','product'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateProductRequest $request, $id)
    {
        $product=Product::findOrFail($id);
        $product->cid=$request->cid;
        $product->title=$request->title;
        $product->keywords=$request->keywords;
        $product->desc=$request->desc;
        $product->remark=$request->remark;
        $product->views=$request->views;
        $product->content=$request->contents;
        if($request->file('pic')){
            $this->exitsimg($product->pic);
            $product->pic=$this->picUploads($request);

        }

        if(isset($request->isshow)){
            $product->isshow=$request->isshow;
        }else{
            $product->isshow=0;
        }
        if($product->save()){
            MyFlash::success('产品编辑成功');
            return redirect()->route('admin.product.index');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request,$id)
    {
        $ids =[];
        if($id!=0){
            $ids[]=$id;
        }else{
            if($request->has('ids')){
                $ids = $request->ids;
            }
        }
        $counts = Product::destroy($ids);
        MyFlash::success('产品删除成功');
        return redirect(route('admin.product.index'));
    }

    //批量删除
    public function destr(Request $request){
        if(isset($request->post()['ids'])){
            $ids=$request->post()['ids'];
            if(Product::destroy($ids)){
                MyFlash::success('删除成功');
                return redirect()->route('admin.product.index');
            }

        }else{
            return redirect()->route('admin.product.index');
        }
    }
    //图片上传
    public function picUploads($request){
        if($request->file('pic')){
            $thumb=$request->pic;
            $name=$thumb->hashName();
            $thumb->storeAs('/public/uploads/',$name);
            return $name;
        }
    }
    // 检查文件是否存在
    public function exitsimg($img){
        $path='public/uploads/'.$img;
        $exists = Storage::disk('local')->exists($path);
        if ($exists) {
            $this->delimg($path);
        }
    }
    // 删除图片
    public function delimg($path){
        $result=Storage::delete($path);
        if ($result) {
            return true;
        }else {
            return false;
        }
    }
}
